import cookies from 'js-cookie'
import ManageConstant from "../consts/manageConstant";

class AuthPermissionService {

  constructor(_this) {
    this._this = _this;
    this._axios = _this.axios.create({
      baseURL: ManageConstant.DOMAIN_URL,
      timeout: 5000,
      headers: {
        Token: cookies.get('Token')
      }
    });
  }

  async testRoleAuth(){
    var isSuccess;
    await this._axios.post(ManageConstant.TEST_AUTH_ROLE).then(res => {
      if (res.data.resultCode == 0) {
        // console.log(0)
        isSuccess = true;
      }
      else if(res.data.resultCode == 102){
        // console.log(102)
        isSuccess = false;
      }
    })
    return isSuccess;
  }

  roleAuth(executeMethod){
    Promise.resolve(this.testRoleAuth()).then(result =>{
      executeMethod(this._this, result);
    })
  }

  async testPermissionAuth(){
    var isSuccess;
    await this._axios.post(ManageConstant.TEST_AUTH_PERMISSION).then(res => {
      if (res.data.resultCode == 0) {
        // console.log(0)
        isSuccess = true;
      }
      else if(res.data.resultCode == 102){
        // console.log(102)
        isSuccess = false;
      }
    })
    return isSuccess;
  }

  permissionAuth(executeMethod){
    Promise.resolve(this.testPermissionAuth()).then(result =>{
      executeMethod(this._this, result);
    })
  }

  async testManageAuth(){
    var isSuccess;
    await this._axios.post(ManageConstant.TEST_AUTH_MANAGE).then(res => {
      if (res.data.resultCode == 0) {
        // console.log(0)
        isSuccess = true;
      }
      else if(res.data.resultCode == 102){
        // console.log(102)
        isSuccess = false;
      }
    })
    return isSuccess;
  }

  manageAuth(executeMethod){
    Promise.resolve(this.testManageAuth()).then(result =>{
      executeMethod(this._this, result);
    })
  }

  async testVideoAuditAuth(){
    var isSuccess;
    await this._axios.post(ManageConstant.TEST_AUTH_VIDEOAUDIT).then(res => {
      if (res.data.resultCode == 0) {
        // console.log(0)
        isSuccess = true;
      }
      else if(res.data.resultCode == 102){
        // console.log(102)
        isSuccess = false;
      }
    })
    return isSuccess;
  }

  videoAuditAuth(executeMethod){
    Promise.resolve(this.testVideoAuditAuth()).then(result =>{
      executeMethod(this._this, result);
    })
  }

}

export default AuthPermissionService;
